

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>agpy.imf &mdash; agpy 0.1.2 documentation</title>
    
    <link rel="stylesheet" href="../../_static/extra.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../',
        VERSION:     '0.1.2',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <link rel="top" title="agpy 0.1.2 documentation" href="../../index.html" />
    <link rel="up" title="agpy" href="../agpy.html" />
     
    <script type="text/javascript">

      var _gaq = _gaq || [];
      _gaq.push(['_setDomainName', 'pyspeckit.bitbucket.org']);
      _gaq.push(['_setAllowHash', false]);
      _gaq.push(['_trackPageview']);


    </script>
    <link rel="stylesheet" type="text/css" href="../../_static/extra.css" />
  </head>
  <body>
    <div class="header-wrapper">
      <div class="header">
        <h1><a href="../../index.html">agpy 0.1.2 documentation</a></h1>
        <div class="rel">
          <a href="http://agpy.googlecode.com">agpy Home </a>  |
          <a href=../../index.html>Docs Home </a>  |
          <a href="http://code.google.com/p/agpy/w/list">Wiki</a>  |
          <a href=../../search.html>Search </a>
        </div>
       </div>
    </div>

    <div class="content-wrapper">
      <div class="content">
        <div class="document">
            
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <h1>Source code for agpy.imf</h1><div class="highlight"><pre>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">Various codes to work with the initial mass function</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">types</span> <span class="c"># I use typechecking.  Is there a better way to do this?  (see inverse_imf below)</span>

<span class="c"># three codes for dn/dlog(m)</span>
<div class="viewcode-block" id="salpeter"><a class="viewcode-back" href="../../agpy.html#agpy.imf.salpeter">[docs]</a><span class="k">def</span> <span class="nf">salpeter</span><span class="p">(</span><span class="n">m</span><span class="p">,</span><span class="n">alpha</span><span class="o">=</span><span class="mf">2.35</span><span class="p">,</span> <span class="n">integral</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    the Salpeter 1955 IMF: dn/dm ~ m^-2.35</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">integral</span><span class="p">:</span> <span class="n">alpha</span> <span class="o">-=</span> <span class="mi">1</span>
    <span class="k">return</span> <span class="n">m</span><span class="o">**</span><span class="p">(</span><span class="o">-</span><span class="n">alpha</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="kroupa"><a class="viewcode-back" href="../../agpy.html#agpy.imf.kroupa">[docs]</a><span class="k">def</span> <span class="nf">kroupa</span><span class="p">(</span><span class="n">m</span><span class="p">,</span><span class="n">integral</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Kroupa 2001 IMF (http://arxiv.org/abs/astro-ph/0009005, http://adsabs.harvard.edu/abs/2001MNRAS.322..231K)</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">exp1</span> <span class="o">=</span> <span class="mf">0.3</span>
    <span class="n">exp2</span> <span class="o">=</span> <span class="mf">1.3</span>
    <span class="n">exp3</span> <span class="o">=</span> <span class="mf">2.3</span>
    <span class="k">if</span> <span class="n">integral</span><span class="p">:</span> 
        <span class="n">exp1</span> <span class="o">+=</span> <span class="mi">1</span>
        <span class="n">exp2</span> <span class="o">-=</span> <span class="mi">1</span>
        <span class="n">exp3</span> <span class="o">-=</span> <span class="mi">1</span>
    <span class="n">zeta</span> <span class="o">=</span> <span class="p">(</span><span class="n">m</span><span class="o">**</span><span class="n">exp1</span> <span class="o">/</span> <span class="mf">0.08</span><span class="o">**</span><span class="n">exp1</span> <span class="o">*</span> <span class="mf">0.08</span><span class="o">**-</span><span class="n">exp2</span><span class="p">)</span><span class="o">*</span><span class="p">(</span><span class="n">m</span><span class="o">&lt;</span><span class="mf">0.08</span><span class="p">)</span>
    <span class="n">zeta</span> <span class="o">+=</span> <span class="p">(</span><span class="n">m</span><span class="o">**-</span><span class="n">exp2</span><span class="p">)</span> <span class="o">*</span> <span class="p">(</span><span class="n">m</span><span class="o">&gt;=</span><span class="mf">0.08</span><span class="p">)</span> <span class="o">*</span> <span class="p">(</span><span class="n">m</span><span class="o">&lt;</span><span class="mf">0.5</span><span class="p">)</span>
    <span class="n">zeta</span> <span class="o">+=</span> <span class="p">(</span><span class="n">m</span><span class="o">**-</span><span class="n">exp3</span> <span class="o">/</span> <span class="mf">0.5</span><span class="o">**-</span><span class="n">exp3</span> <span class="o">*</span> <span class="mf">0.5</span><span class="o">**-</span><span class="n">exp2</span><span class="p">)</span> <span class="o">*</span> <span class="p">(</span><span class="n">m</span><span class="o">&gt;=</span><span class="mf">0.5</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">zeta</span>
</div>
<div class="viewcode-block" id="chabrier"><a class="viewcode-back" href="../../agpy.html#agpy.imf.chabrier">[docs]</a><span class="k">def</span> <span class="nf">chabrier</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">integral</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Chabrier 2003 IMF</span>
<span class="sd">    http://adsabs.harvard.edu/abs/2003PASP..115..763C</span>
<span class="sd">    (only valid for m &lt; 1 msun)</span>

<span class="sd">    not sure which of these to use...</span>

<span class="sd">    integral is NOT IMPLEMENTED</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">integral</span><span class="p">:</span> <span class="k">print</span> <span class="s">&quot;Chabrier integral NOT IMPLEMENTED&quot;</span>
    <span class="c"># This system MF can be parameterized by the same type of lognormal form as</span>
    <span class="c"># the single MF (eq. [17]), with the same normalization at 1 Msun, with the</span>
    <span class="c"># coefficients (Chabrier 2003)</span>
    <span class="k">return</span> <span class="mf">0.86</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="o">*</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">log10</span><span class="p">(</span><span class="n">m</span><span class="p">)</span><span class="o">-</span><span class="n">np</span><span class="o">.</span><span class="n">log10</span><span class="p">(</span><span class="mf">0.22</span><span class="p">))</span><span class="o">**</span><span class="mi">2</span><span class="o">/</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="mf">0.57</span><span class="o">**</span><span class="mi">2</span><span class="p">))</span>
    <span class="c"># This analytic form for the disk MF for single objects below 1 Msun, within these uncertainties, is given by the following lognormal form (Chabrier 2003):</span>
    <span class="k">return</span> <span class="mf">0.158</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="o">*</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">log10</span><span class="p">(</span><span class="n">m</span><span class="p">)</span><span class="o">-</span><span class="n">np</span><span class="o">.</span><span class="n">log10</span><span class="p">(</span><span class="mf">0.08</span><span class="p">))</span><span class="o">**</span><span class="mi">2</span><span class="o">/</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="mf">0.69</span><span class="o">**</span><span class="mi">2</span><span class="p">))</span>
</div>
<div class="viewcode-block" id="schechter"><a class="viewcode-back" href="../../agpy.html#agpy.imf.schechter">[docs]</a><span class="k">def</span> <span class="nf">schechter</span><span class="p">(</span><span class="n">m</span><span class="p">,</span><span class="n">A</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span><span class="n">beta</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span><span class="n">m0</span><span class="o">=</span><span class="mi">100</span><span class="p">,</span> <span class="n">integral</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    A Schechter function with arbitrary defaults</span>
<span class="sd">    (integral may not be correct - exponent hasn&#39;t been dealt with at all)</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">integral</span><span class="p">:</span> <span class="n">beta</span> <span class="o">-=</span> <span class="mi">1</span>
    <span class="k">return</span> <span class="n">A</span><span class="o">*</span><span class="n">m</span><span class="o">**-</span><span class="n">beta</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="n">m</span><span class="o">/</span><span class="n">m0</span><span class="p">)</span>
</div>
<span class="k">try</span><span class="p">:</span> 
    <span class="kn">import</span> <span class="nn">scipy</span>
<div class="viewcode-block" id="schechter_cdf"><a class="viewcode-back" href="../../agpy.html#agpy.imf.schechter_cdf">[docs]</a>    <span class="k">def</span> <span class="nf">schechter_cdf</span><span class="p">(</span><span class="n">m</span><span class="p">,</span><span class="n">A</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span><span class="n">beta</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span><span class="n">m0</span><span class="o">=</span><span class="mi">100</span><span class="p">,</span><span class="n">mmin</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span><span class="n">mmax</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span><span class="n">npts</span><span class="o">=</span><span class="mf">1e4</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return the CDF value of a given mass for a set mmin,mmax</span>
<span class="sd">        mmax will default to 10 m0 if not specified</span>
<span class="sd">        </span>
<span class="sd">        Analytic integral of the Schechter function:</span>
<span class="sd">        http://www.wolframalpha.com/input/?i=integral%28x^-a+exp%28-x%2Fm%29+dx%29</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">mmax</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
            <span class="n">mmax</span> <span class="o">=</span> <span class="mi">10</span><span class="o">*</span><span class="n">m0</span>
        
        <span class="c"># integrate the CDF from the minimum to maximum </span>
        <span class="c"># undefined posint = -m0 * mmax**-beta * (mmax/m0)**beta * scipy.special.gammainc(1-beta, mmax/m0)</span>
        <span class="c"># undefined negint = -m0 * mmin**-beta * (mmin/m0)**beta * scipy.special.gammainc(1-beta, mmin/m0)</span>
        <span class="n">posint</span> <span class="o">=</span> <span class="o">-</span><span class="n">mmax</span><span class="o">**</span><span class="p">(</span><span class="mi">1</span><span class="o">-</span><span class="n">beta</span><span class="p">)</span> <span class="o">*</span> <span class="n">scipy</span><span class="o">.</span><span class="n">special</span><span class="o">.</span><span class="n">expn</span><span class="p">(</span><span class="n">beta</span><span class="p">,</span> <span class="n">mmax</span><span class="o">/</span><span class="n">m0</span><span class="p">)</span>
        <span class="n">negint</span> <span class="o">=</span> <span class="o">-</span><span class="n">mmin</span><span class="o">**</span><span class="p">(</span><span class="mi">1</span><span class="o">-</span><span class="n">beta</span><span class="p">)</span> <span class="o">*</span> <span class="n">scipy</span><span class="o">.</span><span class="n">special</span><span class="o">.</span><span class="n">expn</span><span class="p">(</span><span class="n">beta</span><span class="p">,</span> <span class="n">mmin</span><span class="o">/</span><span class="n">m0</span><span class="p">)</span>
        <span class="n">tot</span> <span class="o">=</span> <span class="n">posint</span><span class="o">-</span><span class="n">negint</span>

        <span class="c"># normalize by the integral</span>
        <span class="c"># undefined ret = (-m0 * m**-beta * (m/m0)**beta * scipy.special.gammainc(1-beta, m/m0)) / tot</span>
        <span class="n">ret</span> <span class="o">=</span> <span class="p">(</span><span class="o">-</span><span class="n">m</span><span class="o">**</span><span class="p">(</span><span class="mi">1</span><span class="o">-</span><span class="n">beta</span><span class="p">)</span> <span class="o">*</span> <span class="n">scipy</span><span class="o">.</span><span class="n">special</span><span class="o">.</span><span class="n">expn</span><span class="p">(</span><span class="n">beta</span><span class="p">,</span> <span class="n">m</span><span class="o">/</span><span class="n">m0</span><span class="p">)</span> <span class="o">-</span> <span class="n">negint</span><span class="p">)</span><span class="o">/</span> <span class="n">tot</span>

        <span class="k">return</span> <span class="n">ret</span>
</div>
<div class="viewcode-block" id="sh_cdf_func"><a class="viewcode-back" href="../../agpy.html#agpy.imf.sh_cdf_func">[docs]</a>    <span class="k">def</span> <span class="nf">sh_cdf_func</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="k">return</span> <span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">schechter_cdf</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span></div>
<span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span>
    <span class="k">pass</span>




<span class="c">#def schechter_inv(m): </span>
<span class="c">#    &quot;&quot;&quot;</span>
<span class="c">#    Return p(m)</span>
<span class="c">#    &quot;&quot;&quot;</span>
<span class="c">#    return scipy.interpolate.interp1d(shfun,arange(.1,20,.01),bounds_error=False,fill_value=20.)</span>

<span class="k">def</span> <span class="nf">integrate</span><span class="p">(</span><span class="n">fn</span><span class="o">=</span><span class="n">kroupa</span><span class="p">,</span> <span class="n">bins</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">logspace</span><span class="p">(</span><span class="o">-</span><span class="mi">2</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">500</span><span class="p">)):</span>
    <span class="n">xax</span> <span class="o">=</span> <span class="p">(</span><span class="n">bins</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="o">+</span><span class="n">bins</span><span class="p">[</span><span class="mi">1</span><span class="p">:])</span><span class="o">/</span><span class="mf">2.</span>
    <span class="n">integral</span> <span class="o">=</span> <span class="p">(</span><span class="n">bins</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span><span class="o">-</span><span class="n">bins</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span> <span class="o">*</span> <span class="p">(</span><span class="n">fn</span><span class="p">(</span><span class="n">bins</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span><span class="o">+</span><span class="n">fn</span><span class="p">(</span><span class="n">bins</span><span class="p">[</span><span class="mi">1</span><span class="p">:]))</span> <span class="o">/</span> <span class="mf">2.</span>

    <span class="k">return</span> <span class="n">xax</span><span class="p">,</span><span class="n">integral</span>

<span class="k">def</span> <span class="nf">m_integrate</span><span class="p">(</span><span class="n">fn</span><span class="o">=</span><span class="n">kroupa</span><span class="p">,</span> <span class="n">bins</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">logspace</span><span class="p">(</span><span class="o">-</span><span class="mi">2</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">500</span><span class="p">)):</span>
    <span class="n">xax</span> <span class="o">=</span> <span class="p">(</span><span class="n">bins</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="o">+</span><span class="n">bins</span><span class="p">[</span><span class="mi">1</span><span class="p">:])</span><span class="o">/</span><span class="mf">2.</span>
    <span class="n">integral</span> <span class="o">=</span> <span class="n">xax</span><span class="o">*</span><span class="p">(</span><span class="n">bins</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span><span class="o">-</span><span class="n">bins</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span> <span class="o">*</span> <span class="p">(</span><span class="n">fn</span><span class="p">(</span><span class="n">bins</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span><span class="o">+</span><span class="n">fn</span><span class="p">(</span><span class="n">bins</span><span class="p">[</span><span class="mi">1</span><span class="p">:]))</span> <span class="o">/</span> <span class="mf">2.</span>

    <span class="k">return</span> <span class="n">xax</span><span class="p">,</span><span class="n">integral</span>

<span class="k">def</span> <span class="nf">cumint</span><span class="p">(</span><span class="n">fn</span><span class="o">=</span><span class="n">kroupa</span><span class="p">,</span> <span class="n">bins</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">logspace</span><span class="p">(</span><span class="o">-</span><span class="mi">2</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">500</span><span class="p">)):</span>
    <span class="n">xax</span><span class="p">,</span><span class="n">integral</span> <span class="o">=</span> <span class="n">integrate</span><span class="p">(</span><span class="n">fn</span><span class="p">,</span><span class="n">bins</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">integral</span><span class="o">.</span><span class="n">cumsum</span><span class="p">()</span> <span class="o">/</span> <span class="n">integral</span><span class="o">.</span><span class="n">sum</span><span class="p">()</span>

<span class="k">def</span> <span class="nf">m_cumint</span><span class="p">(</span><span class="n">fn</span><span class="o">=</span><span class="n">kroupa</span><span class="p">,</span> <span class="n">bins</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">logspace</span><span class="p">(</span><span class="o">-</span><span class="mi">2</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">500</span><span class="p">)):</span>
    <span class="n">xax</span><span class="p">,</span><span class="n">integral</span> <span class="o">=</span> <span class="n">m_integrate</span><span class="p">(</span><span class="n">fn</span><span class="p">,</span><span class="n">bins</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">integral</span><span class="o">.</span><span class="n">cumsum</span><span class="p">()</span> <span class="o">/</span> <span class="n">integral</span><span class="o">.</span><span class="n">sum</span><span class="p">()</span>

<span class="n">massfunctions</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;kroupa&#39;</span><span class="p">:</span><span class="n">kroupa</span><span class="p">,</span> <span class="s">&#39;salpeter&#39;</span><span class="p">:</span><span class="n">salpeter</span><span class="p">,</span> <span class="s">&#39;chabrier&#39;</span><span class="p">:</span><span class="n">chabrier</span><span class="p">,</span> <span class="s">&#39;schechter&#39;</span><span class="p">:</span><span class="n">schechter</span><span class="p">}</span>
<span class="c"># salpeter and schechter selections are arbitrary</span>
<span class="n">mostcommonmass</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;kroupa&#39;</span><span class="p">:</span><span class="mf">0.08</span><span class="p">,</span> <span class="s">&#39;salpeter&#39;</span><span class="p">:</span><span class="mf">0.01</span><span class="p">,</span> <span class="s">&#39;chabrier&#39;</span><span class="p">:</span><span class="mf">0.23</span><span class="p">,</span> <span class="s">&#39;schecter&#39;</span><span class="p">:</span><span class="mf">0.01</span><span class="p">}</span>

<div class="viewcode-block" id="inverse_imf"><a class="viewcode-back" href="../../agpy.html#agpy.imf.inverse_imf">[docs]</a><span class="k">def</span> <span class="nf">inverse_imf</span><span class="p">(</span><span class="n">p</span><span class="p">,</span> <span class="n">nbins</span><span class="o">=</span><span class="mi">1000</span><span class="p">,</span> <span class="n">mmin</span><span class="o">=</span><span class="mf">0.03</span><span class="p">,</span> <span class="n">mmax</span><span class="o">=</span><span class="mi">120</span><span class="p">,</span> <span class="n">massfunc</span><span class="o">=</span><span class="s">&#39;kroupa&#39;</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Inverse mass function</span>

<span class="sd">    massfunc can be &#39;kroupa&#39;, &#39;chabrier&#39;, &#39;salpeter&#39;, &#39;schechter&#39;, or a function</span>
<span class="sd">    &quot;&quot;&quot;</span>
 
    <span class="n">masses</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">logspace</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">log10</span><span class="p">(</span><span class="n">mmin</span><span class="p">),</span><span class="n">np</span><span class="o">.</span><span class="n">log10</span><span class="p">(</span><span class="n">mmax</span><span class="p">),</span><span class="n">nbins</span><span class="p">)</span>
    <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">massfunc</span><span class="p">)</span> <span class="ow">is</span> <span class="n">types</span><span class="o">.</span><span class="n">FunctionType</span><span class="p">:</span>
        <span class="n">mf</span> <span class="o">=</span> <span class="n">massfunc</span><span class="p">(</span><span class="n">masses</span><span class="p">,</span> <span class="n">integral</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
    <span class="k">elif</span> <span class="nb">type</span><span class="p">(</span><span class="n">massfunc</span><span class="p">)</span> <span class="ow">is</span> <span class="nb">str</span><span class="p">:</span>
        <span class="n">mf</span> <span class="o">=</span> <span class="n">massfunctions</span><span class="p">[</span><span class="n">massfunc</span><span class="p">](</span><span class="n">masses</span><span class="p">,</span> <span class="n">integral</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s">&quot;massfunc must either be a string in the set </span><span class="si">%s</span><span class="s"> or a function&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="s">&quot;,&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">massfunctions</span><span class="o">.</span><span class="n">keys</span><span class="p">())))</span>
    <span class="n">mfcum</span> <span class="o">=</span> <span class="n">mf</span><span class="o">.</span><span class="n">cumsum</span><span class="p">()</span>
    <span class="n">mfcum</span> <span class="o">/=</span> <span class="n">mfcum</span><span class="o">.</span><span class="n">max</span><span class="p">()</span> <span class="c"># normalize to sum (cdf)</span>

    <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">interp</span><span class="p">(</span><span class="n">p</span><span class="p">,</span> <span class="n">mfcum</span><span class="p">,</span> <span class="n">masses</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="make_cluster"><a class="viewcode-back" href="../../agpy.html#agpy.imf.make_cluster">[docs]</a><span class="k">def</span> <span class="nf">make_cluster</span><span class="p">(</span><span class="n">mcluster</span><span class="p">,</span> <span class="n">massfunc</span><span class="o">=</span><span class="s">&#39;kroupa&#39;</span><span class="p">,</span> <span class="n">verbose</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">silent</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">tolerance</span><span class="o">=</span><span class="mf">0.5</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Sample from an IMF to make a cluster.  Returns the masses of all stars in the cluster</span>
<span class="sd">    </span>
<span class="sd">    massfunc must be a string </span>
<span class="sd">    tolerance is how close the cluster mass must be to the requested mass.  </span>
<span class="sd">    If the last star is greater than this tolerance, the total mass will not be within</span>
<span class="sd">    tolerance of the requested</span>

<span class="sd">    kwargs are passed to `inverse_imf`</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="c"># use most common mass to guess needed number of samples</span>
    <span class="n">nsamp</span> <span class="o">=</span> <span class="n">mcluster</span> <span class="o">/</span> <span class="n">mostcommonmass</span><span class="p">[</span><span class="n">massfunc</span><span class="p">]</span>
    <span class="n">masses</span> <span class="o">=</span> <span class="n">inverse_imf</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">random</span><span class="p">(</span><span class="n">nsamp</span><span class="p">),</span> <span class="n">massfunc</span><span class="o">=</span><span class="n">massfunc</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

    <span class="n">mtot</span> <span class="o">=</span> <span class="n">masses</span><span class="o">.</span><span class="n">sum</span><span class="p">()</span>
    <span class="k">if</span> <span class="n">verbose</span><span class="p">:</span> <span class="k">print</span> <span class="s">&quot;</span><span class="si">%i</span><span class="s"> samples yielded a cluster mass of </span><span class="si">%g</span><span class="s"> (</span><span class="si">%g</span><span class="s"> requested)&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">nsamp</span><span class="p">,</span><span class="n">mtot</span><span class="p">,</span><span class="n">mcluster</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">mtot</span> <span class="o">&gt;</span> <span class="n">mcluster</span> <span class="o">+</span> <span class="n">tolerance</span><span class="p">:</span>
        <span class="n">mcum</span> <span class="o">=</span> <span class="n">masses</span><span class="o">.</span><span class="n">cumsum</span><span class="p">()</span>
        <span class="n">last_ind</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">argmax</span><span class="p">(</span><span class="n">mcum</span> <span class="o">&gt;</span> <span class="n">mcluster</span><span class="p">)</span>
        <span class="n">masses</span> <span class="o">=</span> <span class="n">masses</span><span class="p">[:</span><span class="n">last_ind</span><span class="p">]</span>
        <span class="n">mtot</span> <span class="o">=</span> <span class="n">masses</span><span class="o">.</span><span class="n">sum</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">verbose</span><span class="p">:</span> <span class="k">print</span> <span class="s">&quot;Selected the first </span><span class="si">%i</span><span class="s"> out of </span><span class="si">%i</span><span class="s"> masses to get </span><span class="si">%g</span><span class="s"> total&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">last_ind</span><span class="p">,</span><span class="nb">len</span><span class="p">(</span><span class="n">mcum</span><span class="p">),</span><span class="n">mtot</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">while</span> <span class="n">mtot</span> <span class="o">&lt;</span> <span class="n">mcluster</span><span class="p">:</span>
            <span class="c"># at least 1 sample, but potentially many more</span>
            <span class="n">nsamp</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">ceil</span><span class="p">((</span><span class="n">mcluster</span><span class="o">-</span><span class="n">mtot</span><span class="p">)</span> <span class="o">/</span> <span class="n">mostcommonmass</span><span class="p">[</span><span class="n">massfunc</span><span class="p">])</span>
            <span class="n">newmasses</span> <span class="o">=</span> <span class="n">inverse_imf</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">random</span><span class="p">(</span><span class="n">nsamp</span><span class="p">),</span> <span class="n">massfunc</span><span class="o">=</span><span class="n">massfunc</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
            <span class="n">masses</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">concatenate</span><span class="p">([</span><span class="n">masses</span><span class="p">,</span><span class="n">newmasses</span><span class="p">])</span>
            <span class="n">mtot</span> <span class="o">=</span> <span class="n">masses</span><span class="o">.</span><span class="n">sum</span><span class="p">()</span>
            <span class="k">if</span> <span class="n">verbose</span><span class="p">:</span> <span class="k">print</span> <span class="s">&quot;Sampled </span><span class="si">%i</span><span class="s"> new stars.  Total is now </span><span class="si">%g</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">nsamp</span><span class="p">,</span> <span class="n">mtot</span><span class="p">)</span>

            <span class="k">if</span> <span class="n">mtot</span> <span class="o">&gt;</span> <span class="n">mcluster</span><span class="o">+</span><span class="n">tolerance</span><span class="p">:</span> <span class="c"># don&#39;t force exact equality; that would yield infinite loop</span>
                <span class="n">mcum</span> <span class="o">=</span> <span class="n">masses</span><span class="o">.</span><span class="n">cumsum</span><span class="p">()</span>
                <span class="n">last_ind</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">argmax</span><span class="p">(</span><span class="n">mcum</span> <span class="o">&gt;</span> <span class="n">mcluster</span><span class="p">)</span>
                <span class="n">masses</span> <span class="o">=</span> <span class="n">masses</span><span class="p">[:</span><span class="n">last_ind</span><span class="p">]</span>
                <span class="n">mtot</span> <span class="o">=</span> <span class="n">masses</span><span class="o">.</span><span class="n">sum</span><span class="p">()</span>
                <span class="k">if</span> <span class="n">verbose</span><span class="p">:</span> <span class="k">print</span> <span class="s">&quot;Selected the first </span><span class="si">%i</span><span class="s"> out of </span><span class="si">%i</span><span class="s"> masses to get </span><span class="si">%g</span><span class="s"> total&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">last_ind</span><span class="p">,</span><span class="nb">len</span><span class="p">(</span><span class="n">mcum</span><span class="p">),</span><span class="n">mtot</span><span class="p">)</span>

    <span class="k">if</span> <span class="ow">not</span> <span class="n">silent</span><span class="p">:</span> <span class="k">print</span> <span class="s">&quot;Total cluster mass is </span><span class="si">%g</span><span class="s"> (limit was </span><span class="si">%g</span><span class="s">)&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">mtot</span><span class="p">,</span><span class="n">mcluster</span><span class="p">)</span>

    <span class="k">return</span> <span class="n">masses</span>

<span class="c"># Vacca Garmany Shull log(lyman continuum) parameters</span>
<span class="c"># Power-law extrapolated from 18 to 8 and from 50 to 150</span>
<span class="c"># (using, e.g., &quot;,&quot;.join([&quot;%0.2f&quot; % p for p in polyval(polyfit(log10(vgsmass[:5]),vgslogq[:5],1),log10(linspace(50,150,6)))[::-1]]) </span>
<span class="c"># where vgsmass does *not* include the extrapolated values)</span></div>
<span class="n">vgsmass</span> <span class="o">=</span> <span class="p">[</span><span class="mf">150.</span><span class="p">,</span>  <span class="mf">130.</span><span class="p">,</span>  <span class="mf">110.</span><span class="p">,</span>   <span class="mf">90.</span><span class="p">,</span>   <span class="mf">70.</span><span class="p">,</span>  <span class="mf">51.3</span><span class="p">,</span><span class="mf">44.2</span><span class="p">,</span><span class="mf">41.0</span><span class="p">,</span><span class="mf">38.1</span><span class="p">,</span><span class="mf">35.5</span><span class="p">,</span><span class="mf">33.1</span><span class="p">,</span><span class="mf">30.8</span><span class="p">,</span><span class="mf">28.8</span><span class="p">,</span><span class="mf">26.9</span><span class="p">,</span><span class="mf">25.1</span><span class="p">,</span><span class="mf">23.6</span><span class="p">,</span><span class="mf">22.1</span><span class="p">,</span><span class="mf">20.8</span><span class="p">,</span><span class="mf">19.5</span><span class="p">,</span><span class="mf">18.4</span><span class="p">,</span><span class="mf">18.</span><span class="p">,</span>  <span class="mf">16.</span><span class="p">,</span>  <span class="mf">14.</span><span class="p">,</span>  <span class="mf">12.</span><span class="p">,</span>  <span class="mf">10.</span><span class="p">,</span>   <span class="mf">8.</span><span class="p">][::</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
<span class="n">vgslogq</span> <span class="o">=</span> <span class="p">[</span><span class="mf">50.51</span><span class="p">,</span><span class="mf">50.34</span><span class="p">,</span><span class="mf">50.13</span><span class="p">,</span><span class="mf">49.88</span><span class="p">,</span><span class="mf">49.57</span><span class="p">,</span><span class="mf">49.18</span><span class="p">,</span><span class="mf">48.99</span><span class="p">,</span><span class="mf">48.90</span><span class="p">,</span><span class="mf">48.81</span><span class="p">,</span><span class="mf">48.72</span><span class="p">,</span><span class="mf">48.61</span><span class="p">,</span><span class="mf">48.49</span><span class="p">,</span><span class="mf">48.34</span><span class="p">,</span><span class="mf">48.16</span><span class="p">,</span><span class="mf">47.92</span><span class="p">,</span><span class="mf">47.63</span><span class="p">,</span><span class="mf">47.25</span><span class="p">,</span><span class="mf">46.77</span><span class="p">,</span><span class="mf">46.23</span><span class="p">,</span><span class="mf">45.69</span><span class="p">,</span><span class="mf">45.58</span><span class="p">,</span><span class="mf">44.65</span><span class="p">,</span><span class="mf">43.60</span><span class="p">,</span><span class="mf">42.39</span><span class="p">,</span><span class="mf">40.96</span><span class="p">,</span><span class="mf">39.21</span><span class="p">][::</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>

<div class="viewcode-block" id="lyc_of_star"><a class="viewcode-back" href="../../agpy.html#agpy.imf.lyc_of_star">[docs]</a><span class="k">def</span> <span class="nf">lyc_of_star</span><span class="p">(</span><span class="n">mass</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Determine lyman continuum luminosity of a star given its mass</span>
<span class="sd">    Uses the Vacca, Garmany, Shull 1996 Table 5 Log Q and Mspec parameters</span>

<span class="sd">    returns LogQ</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">interp</span><span class="p">(</span><span class="n">mass</span><span class="p">,</span> <span class="n">vgsmass</span><span class="p">,</span> <span class="n">vgslogq</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="lyc_of_cluster"><a class="viewcode-back" href="../../agpy.html#agpy.imf.lyc_of_cluster">[docs]</a><span class="k">def</span> <span class="nf">lyc_of_cluster</span><span class="p">(</span><span class="n">masses</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Determine the log of the integrated lyman continuum luminosity of a cluster</span>
<span class="sd">    Only M&gt;=8msun count</span>

<span class="sd">    masses is a list or array of masses.  </span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="nb">max</span><span class="p">(</span><span class="n">masses</span><span class="p">)</span> <span class="o">&lt;</span> <span class="mi">8</span><span class="p">:</span> <span class="k">return</span> <span class="mi">0</span>
    <span class="n">logq</span> <span class="o">=</span> <span class="n">lyc_of_star</span><span class="p">(</span><span class="n">masses</span><span class="p">[</span><span class="n">masses</span> <span class="o">&gt;=</span> <span class="mi">8</span><span class="p">])</span>
    <span class="n">logqtot</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">log10</span><span class="p">(</span> <span class="p">(</span><span class="mi">10</span><span class="o">**</span><span class="n">logq</span><span class="p">)</span><span class="o">.</span><span class="n">sum</span><span class="p">()</span> <span class="p">)</span>
    <span class="k">return</span> <span class="n">logqtot</span></div>
</pre></div>

          </div>
        </div>
      </div>
        </div>
        <div class="sidebar">
          <h3>Table Of Contents</h3>
          <ul>
<li class="toctree-l1"><a class="reference internal" href="../../agpy.html">Adam Ginsburg&#8217;s Python Code (agpy)</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../image_tools.html">Image Tools</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../fft_tools.html">AG_fft_tools Package</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../plfit.html">plfit Package</a></li>
</ul>

          <h3 style="margin-top: 1.5em;">Search</h3>
          <form class="search" action="../../search.html" method="get">
            <input type="text" name="q" />
            <input type="submit" value="Go" />
            <input type="hidden" name="check_keywords" value="yes" />
            <input type="hidden" name="area" value="default" />
          </form>
          <p class="searchtip" style="font-size: 90%">
            Enter search terms or a module, class or function name.
          </p>
        </div>
        <div class="clearer"></div>
      </div>
    </div>

    <div class="footer">
      &copy; Copyright 2011, Adam Ginsburg.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.2pre.
    <script type="text/javascript">

      var _gaq = _gaq || [];
      _gaq.push(['_setAccount', 'UA-6253248-2']);
      _gaq.push(['_trackPageview']);

      (function() {
        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
      })();

    </script>
        
    </div>
  </body>
</html>